<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="login-container">
    <!-- 动态背景 -->
    <div class="background-animation">
      <div class="gradient-orb orb-1"></div>
      <div class="gradient-orb orb-2"></div>
      <div class="gradient-orb orb-3"></div>
    </div>

    <!-- 登录卡片 -->
    <div class="login-box">
      <!-- Logo和标题 -->
      <div class="login-header">
        <div class="logo-icon">🚄</div>
        <h2 class="system-title">火车票售票系统</h2>
        <p class="system-subtitle">Railway Ticket Management System</p>
      </div>

      <!-- 登录表单 -->
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="loginFormRef"
        @keyup.enter="handleLogin"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            prefix-icon="User"
            clearable
            class="form-input"
          >
            <template #prefix>
              <el-icon class="input-icon"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            prefix-icon="Lock"
            show-password
            clearable
            class="form-input"
          >
            <template #prefix>
              <el-icon class="input-icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            @click="handleLogin"
            :loading="loading"
            class="login-button"
          >
            <span v-if="!loading">立即登录</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 演示账号信息 -->
      <div class="demo-info">
        <div class="demo-title">
          <el-icon><InfoFilled /></el-icon>
          <span>演示账号</span>
        </div>
        <div class="demo-accounts">
          <div class="demo-account">
            <el-tag type="danger" size="small">管理员</el-tag>
            <span>admin / admin123</span>
          </div>
          <div class="demo-account">
            <el-tag type="success" size="small">普通用户</el-tag>
            <span>user001 / 123456</span>
          </div>
        </div>
      </div>

      <!-- 页脚 -->
      <div class="login-footer">
        <p>© 2025 Railway Ticket System. All rights reserved.</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, InfoFilled } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)
const loginFormRef = ref()

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const handleLogin = async () => {
  try {
    const valid = await loginFormRef.value.validate()
    if (!valid) return

    loading.value = true

    const response = await userStore.login(loginForm)

    if (response.success) {
      ElMessage.success('登录成功')
      // 根据用户角色跳转到不同页面
      if (userStore.isAdmin()) {
        router.push('/trains')
      } else {
        router.push('/purchase')
      }
    } else {
      ElMessage.error(response.message)
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error('登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--gradient-primary);
  position: relative;
  overflow: hidden;
}

/* ==================== 动态背景 ==================== */

.background-animation {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;
}

.gradient-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.6;
  animation: float 20s ease-in-out infinite;
}

.orb-1 {
  width: 500px;
  height: 500px;
  background: var(--primary-light);
  top: -10%;
  left: -10%;
  animation-delay: 0s;
}

.orb-2 {
  width: 400px;
  height: 400px;
  background: var(--secondary-light);
  bottom: -10%;
  right: -10%;
  animation-delay: 7s;
}

.orb-3 {
  width: 350px;
  height: 350px;
  background: var(--accent-light);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation-delay: 14s;
}

@keyframes float {
  0%,
  100% {
    transform: translate(0, 0) scale(1);
  }
  33% {
    transform: translate(50px, -50px) scale(1.1);
  }
  66% {
    transform: translate(-50px, 50px) scale(0.9);
  }
}

/* ==================== 登录卡片 ==================== */

.login-box {
  width: 450px;
  padding: var(--spacing-10);
  background: var(--glass-bg);
  backdrop-filter: var(--glass-backdrop);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-2xl);
  box-shadow: var(--shadow-2xl);
  position: relative;
  z-index: 1;
  animation: scaleIn 0.5s var(--ease-out);
}

.login-box::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: var(--gradient-primary);
  border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
}

/* ==================== Logo和标题 ==================== */

.login-header {
  text-align: center;
  margin-bottom: var(--spacing-8);
}

.logo-icon {
  font-size: 4rem;
  margin-bottom: var(--spacing-4);
  animation: pulse 2s ease-in-out infinite;
  display: inline-block;
}

.system-title {
  margin: 0 0 var(--spacing-2) 0;
  color: var(--text-primary);
  font-size: var(--font-size-3xl);
  font-weight: var(--font-weight-bold);
  background: var(--gradient-primary);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.system-subtitle {
  margin: 0;
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-normal);
  letter-spacing: 0.5px;
}

/* ==================== 登录表单 ==================== */

.login-form {
  margin-bottom: var(--spacing-6);
}

.form-input {
  transition: all var(--transition-base);
}

.form-input :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border-light);
  box-shadow: var(--shadow-xs);
  transition: all var(--transition-base);
}

.form-input :deep(.el-input__wrapper:hover) {
  border-color: var(--primary-light);
  box-shadow: var(--shadow-sm);
}

.form-input :deep(.el-input__wrapper.is-focus) {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 4px rgba(0, 180, 216, 0.1);
}

.input-icon {
  color: var(--primary-color);
  font-size: var(--font-size-lg);
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-semibold);
  background: var(--gradient-primary);
  border: none;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-primary);
  transition: all var(--transition-base);
  position: relative;
  overflow: hidden;
}

.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transition: left 0.5s;
}

.login-button:hover::before {
  left: 100%;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.login-button:active {
  transform: translateY(0);
}

/* ==================== 演示信息 ==================== */

.demo-info {
  padding: var(--spacing-5);
  background: linear-gradient(135deg, rgba(0, 180, 216, 0.05), rgba(6, 214, 160, 0.05));
  border: 1px solid var(--border-light);
  border-radius: var(--radius-lg);
  margin-bottom: var(--spacing-6);
}

.demo-title {
  display: flex;
  align-items: center;
  gap: var(--spacing-2);
  margin-bottom: var(--spacing-3);
  color: var(--text-primary);
  font-weight: var(--font-weight-semibold);
  font-size: var(--font-size-sm);
}

.demo-title .el-icon {
  color: var(--info-color);
  font-size: var(--font-size-lg);
}

.demo-accounts {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-2);
}

.demo-account {
  display: flex;
  align-items: center;
  gap: var(--spacing-3);
  padding: var(--spacing-2) var(--spacing-3);
  background: rgba(255, 255, 255, 0.6);
  border-radius: var(--radius-base);
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  transition: all var(--transition-base);
}

.demo-account:hover {
  background: rgba(255, 255, 255, 0.9);
  transform: translateX(4px);
}

/* ==================== 页脚 ==================== */

.login-footer {
  text-align: center;
  padding-top: var(--spacing-4);
  border-top: 1px solid var(--border-light);
}

.login-footer p {
  margin: 0;
  color: var(--text-tertiary);
  font-size: var(--font-size-xs);
}

/* ==================== 响应式适配 ==================== */

@media (max-width: 768px) {
  .login-box {
    width: 90%;
    padding: var(--spacing-6);
  }

  .system-title {
    font-size: var(--font-size-2xl);
  }

  .logo-icon {
    font-size: 3rem;
  }

  .orb-1,
  .orb-2,
  .orb-3 {
    width: 300px;
    height: 300px;
  }
}
</style>
